/*NOTE TO JESSE!!!
The first 4 functions (fightProcedure, fightMenu, fightAlgorithm & fightCreatureAlgorithm)
are working and fine. Fight procedure, you might want to take a look at to get
wtf is going on in this. The 2 LAST functions (DefencePercentage is in the middle, 
ignore it, I just got lazy when I couldn't keep track of shit) are what you should
look at.

tl;dr Look at 2nd last function 'WeaponDegeneration'*/

void Fight::fightProcedure()	{
	fstream inFile;
	int lineNumber;

	//Decide the creature's general area
	int creatureType = InitializeObject.randomNumber(1, 10);
	if(creatureType >= 5)	{
		creatureType = InitializeObject.area;
	}

	//Decide what the creature is in the area chosen
	creatureType = InitializeObject.randomNumber(creatureType * 4 - 3, creatureType * 4);

	//Assign the creature stats
	CreaturesObject.attributeAssigner(creatureType);

	cout << "A " << CreaturesObject.CreatureDescription << " jumps out at you\n" << endl;

	//Go through the fight menu as long as both are alive, and you haven't escaped
	while (PlayerObject.Health > 0 && CreaturesObject.Health > 0 && PlayerObject.run == false)	{
		//Player fight options
		fightMenu();
		//If you haven't escaped, the creature attacks you
		if (PlayerObject.run == false && CreaturesObject.Health > 0)	{
			//Creature fight procedure
			CreatureFightAlgorithm();
		}
		//Output player and creature health
		cout << "Player's Health = " << PlayerObject.Health << endl;
		cout << CreaturesObject.CreatureDescription << "'s Health = " << CreaturesObject.Health << endl << endl;
	}

	//If you've killed the creature, output so
	if (PlayerObject.Health > 0 && PlayerObject.run == false)	{
		cout << "You've killed the " << CreaturesObject.CreatureDescription << endl;
		//Chance of a dead monster dropping an item
		if (InitializeObject.randomNumber(0, 5) == 1)	{
			//Choose item randomly from CompInv file list
			inFile.open("CompInv.txt");
			inFile >> lineNumber;
			inFile.close();

			int item = InitializeObject.randomNumber(1, lineNumber + 1);
			InvObject.dropItem(lineNumber);
		}
	}
	PlayerObject.run = false;
}

void Fight::fightMenu()	{
	InitializeObject.choice = NULL;

	cout << "1.Attack\n";
	cout << "2.Items\n";
	cout << "3.Run\n" << endl;

	while (InitializeObject.choice == NULL)	{
		cin >> InitializeObject.choice;
		switch (InitializeObject.choice)	{
			case 061:
				FightObject.fightAlgorithm();
				break;
			case 062:
				InvObject.Inventory();
				break;
			case 063:
				if (InitializeObject.randomNumber(1, 3) == 1)	{
					cout << "You manage to escape\n" << endl;
					PlayerObject.run = true;
				}
				else cout << "You fail to escape\n" << endl;
				break;
			default:
				cout << "Invalid character\n" << endl;
				InitializeObject.choice = NULL;
		}
	}
}

void Fight::fightAlgorithm()	{
	//Player Damage Done
	cout << "You attack the " << CreaturesObject.CreatureDescription << endl;
	//Determine the damage, based on the opponent's defence and player's strength
	PlayerObject.Damage = InitializeObject.randomNumber(InvObject.AttackBonus - (CreaturesObject.DefenceBonus/3), InvObject.AttackBonus);
	//If damage is a negative number, neutralize it
	if (PlayerObject.Damage < 0)	{
		PlayerObject.Damage = 0;
	}
	
	//Determine if you hit
	if (InitializeObject.randomNumber(0, 1))	{
		cout << "The creature dodged your attack\n" << endl;
		PlayerObject.Damage = 0;
	}

	//Display damage done
	else	{
		cout << "You did " << PlayerObject.Damage << " points of damage to the " << CreaturesObject.CreatureDescription << endl << endl;
		CreaturesObject.Health -= PlayerObject.Damage;
		//Reduce weapon quality
		InvObject.WeaponDegeneration();
	}
}

//___________________________________________________________________________________________________________________________
void Fight::CreatureFightAlgorithm()	{
	//Creature Damage Done
		cout << "The " << CreaturesObject.CreatureDescription << " attacks you" << endl;
		//Creature damage, based on strength and player's defence
		CreaturesObject.Damage = InitializeObject.randomNumber(CreaturesObject.AttackBonus - (InvObject.DefenceBonus ), CreaturesObject.AttackBonus);
		
		if (CreaturesObject.Damage < 0)	{
			CreaturesObject.Damage = 0;
		}
		
		//if the creature hits or not
		if (InitializeObject.randomNumber(0, 2))	{
			cout << "You dodge the creature's attack\n" << endl;
			CreaturesObject.Damage = 0;
		}
		else	{
			cout << "The creature did " << CreaturesObject.Damage << " points of damage to you\n" << endl;
			PlayerObject.Health -= CreaturesObject.Damage;

			if (CreaturesObject.Damage > 0)	{
				InvObject.ArmorDegeneration(CreaturesObject.Damage);
			}
		}
}

//##############################ITEMS################################################

int Inv::DefencePercentage ()	{
	InvObject.DefenceBonus = InvObject.HelmetBonus + InvObject.ShieldBonus + InvObject.BodyMailBonus + InvObject.LeggingsBonus;
	return ((InvObject.DefenceBonus/InvObject.EquippedNumber)/100);
}

void Inv::WeaponDegeneration ()	{
	if	(CurrentEquipped[1] > 0)	{

		int Degredation = InitializeObject.randomNumber(1, 3);
		//THE BELOW LINE IS AN ERROR! FIX IT!!!
		if (AmountInventory[CurrentEquipped[1]] > 1)	{	//If there's more than one
			AmountInventory[CurrentEquipped[1]] -= 1;	//Erase one amount

			//add in a new one at the end
			AmountInventory.push_back(1);
			CurrentInventory.push_back(CurrentInventory[CurrentEquipped[1]]);
			ItemQuality.push_back(ItemQuality[CurrentEquipped[1]]);
	
			//Make the new item the equipped one
			CurrentEquipped[1] = CurrentInventory.size() - 1;
		}

		//Decrease it's quality
		ItemQuality[CurrentEquipped[1]] -= Degredation;
	}
}

void Inv::ArmorDegeneration (int Damage)	{
	if (EquippedNumber)	{
		int Degredation = Damage/EquippedNumber;

		for (int i=0; i<7; i++)	{
			if (CurrentEquipped[i])	{
				AmountInventory[CurrentEquipped[i]]--;	//Erase one amount
	
				//add in a new one at the end
				AmountInventory.push_back(i);
				CurrentInventory.push_back(CurrentInventory[CurrentEquipped[i]]);
				ItemQuality.push_back(ItemQuality[CurrentEquipped[i]]);

				//Make the new item the equipped one
				CurrentEquipped[i] = CurrentInventory.size();

				//Decrease the new item's quality
				ItemQuality[CurrentEquipped[i]] -= Degredation;
			}
		}	
	}
}